<?php
require_once "funct.getLists.php";
require_once "funct.getFiles.php";

class rglLista {
	public $tipoLista = '';
	public $codLista = '';
	public $descr = '';
	public $regoleDir = '';
	public $aFiles = array();
	public $aCampiList = array();
	public $aCampiDB = array();
	public $aCampiDBDef = array();
	public $sPagiFilt = '';
	public $sOrder = '';
	public $oDocGen = null;
	public $errno = 0;
	public $error = '';
	
	public function __construct($tipoLista,$codLista='') {
		global $objSess, $objDB;
		$this->tipoLista = $tipoLista;
		if ($codLista=='') $codLista = "default";
		$this->codLista = $codLista;
		if (!$aList = getLists($tipoLista)) return $this->setErr(10,"tipo lista $tipoLista inesistente");
		$this->descr = $aList['descr'];
		$this->regoleDir = $aList['dir'];
		$this->aFiles = array();
		foreach ($aList['files'] as $file => $aX) {
			if ($aFile = getFiles($file)) {
				$this->aFiles[$file] = $aFile;
				$this->aFiles[$file]['alias'] = $aX['alias'];
				$aDef = getDefCampi($file);
				foreach($aDef as $campoY => $aY) {
					$this->aCampiDB[$aX['alias']."_".$campoY] = '';
					$this->aCampiDBDef[$aX['alias']."_".$campoY] = $aDef[$campoY];
				}
			}
		}
		$sSql = "SELECT * FROM rgl_campi_list "
			." WHERE tipo_list = '".addslashes($tipoLista)."'"
			." AND cod_list = '".addslashes($codLista)."'"
			." ORDER BY seq ";
		$objDB->query($sSql,'selOvfLista');
		$this->aCampiList = array();
		while($aCX = $objDB->fetch('selOvfLista')) {
			$this->aCampiList[] = $aCX;
		}
		if (empty($this->aCampiList)) {
			return $this->setErr(10,'manca def. lista');
		}
		$totSlot = 0;
		foreach($this->aCampiList as $kX =>$aCX) {
			if ($totSlot >= 20) {
				unset($this->aCampiList[$kX]);
				continue;
			}
			$cols = $aCX['campo_size'];
			$totSlot += $cols;
			if ($totSlot > 20) {
				$this->aCampiList[$kX]['campo_size'] = $cols;
			}			
		}
	}
	
	private function setErr($err=0,$des='') {
		$this->errno = $err;
		$this->error = $des;
		return ($err==0);
	}
	
	public function getCampiSelect($alias='') {
		$sSql = '';
		foreach($this->aCampiDBDef as $campoX => $aDefX) {
			if ($alias != '' && $campoX[0] != $alias) continue;
			$campoZ = $campoX;
			$campoZ[1] = '.';
			$sSql .= "$campoZ AS $campoX, ";
		}
		return substr($sSql,0,-2);
	}
	
	private function eseguiRegole($regola) {
		$scriptName = $this->tipoLista."_".$this->codLista."_".$regola.".php";
		$regolaStdPath = RGL_FS_WEB_ROOT."/app".$this->regoleDir."/_reg/$scriptName";
		$regolaPerPath = RGL_FS_WEB_ROOT."/pers/regole/".$this->regoleDir."/$scriptName";
		$sRet = '';
		if (file_exists($regolaStdPath)) {
			$script = file_get_contents($regolaStdPath);
			$posFun = strpos($script,'function ');
			$posPar = strpos($script,'{',$posFun);
			if ($posFun !== false && $posPar !== false) {
				$nomeFunz = uniqid($regola);
				$script = "function $nomeFunz(&\$aCampiDB)".substr($script,$posPar);
				eval($script);
				$sRet .= $nomeFunz($this->aCampiDB);
			}
		}
		if (file_exists($regolaPerPath)) {
			$script = file_get_contents($regolaPerPath);
			$posFun = strpos($script,'function ');
			$posPar = strpos($script,'{',$posFun);
			if ($posFun !== false && $posPar !== false) {
				$nomeFunz = uniqid($regola);
				$script = "function $nomeFunz(&\$aCampiDB)".substr($script,$posPar);
				eval($script);
				$sRet .= $nomeFunz($this->aCampiDB);
			}
		}
		return $sRet;
	}
	
	public function getHtmlLab($pagSessVar='', $sQS='') {
		global $objSess, $objDB, $sXSNQS;
		if ($pagSessVar != '') {
			$campoOrd = $objSess->setReqSV($pagSessVar,'campoOrd','');
			$tipoOrd = $objSess->setReqSV($pagSessVar, 'tipoOrd', 'ASC');
		}
		$sHtml = '';
		//dbg_("getHtmlLab: $pagSessVar, $sQS");
		foreach($this->aCampiList as $kX =>$aCX) {
			$cols = $aCX['campo_size'];
			if ($pagSessVar == '' || substr($aCX['campo'],0,2) == "W_") {
				$sHtml .= "<td class='lab1' colspan='$cols' align='center'>".$aCX['label']."</td>";
				continue;
			}
			$campoX = $campoOrdX = $aCX['campo'];
			$campoOrdX[1] = '.';
			if ($campoOrd == '') {
				$campoOrd = $objSess->setReqSV($pagSessVar,'campoOrd',$campoOrdX);
			}
			$sHtml .= "<td class='lab1' colspan='$cols' align='center'>";
			if ($sQS=='') $sQS .= "&";
			$sHref = $_SERVER['PHP_SELF']."?campoOrd=$campoOrdX&$sQS$sXSNQS";
			if ($campoOrdX != $campoOrd) {
				$sHtml .= "<a href='$sHref&tipoOrd=ASC' class='lab1'>".$aCX['label']."</a></td>";
				continue;
			}
			if ($tipoOrd=='DESC') {
				$sHtml .= "<a href='$sHref&tipoOrd=ASC' class='lab1'>";
				$sHtml .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_giu.gif' border='0'/>&nbsp;";
			} else {
				$sHtml .= "<a href='$sHref&tipoOrd=DESC' class='lab1'>";
				$sHtml .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_su.gif' border='0'/>&nbsp;";
			}		
			$sHtml .= $aCX['label']."</a></td>";
		}
		$this->sPagiFilt = '';
		$this->sOrder = '';
		if ($campoOrd != '') {
			$this->sOrder = " ORDER BY $campoOrd $tipoOrd ";
		}
		$sX = '(';
		foreach($this->aCampiList as $kX => $aX) {
			if (substr($aX['campo'],0,2)!='A_') continue;
			$sX .= "A.".substr($aX['campo'],2)." LIKE '%".addslashes(reqVar('pagifilt'))."%' OR ";
		}
		if (reqVar('pagifilt')!='' && $sX != '(') {
			$this->sPagiFilt = substr($sX,0,-4).')';
		}
		
		return $sHtml;
	}
		
	public function getHtmlDat($aRow,$aOpt) {
		global $objDB;
		$this->aCampiDB = $aRow;
		$this->eseguiRegole("listcalc");
		$sClass = (rglVx($aOpt,"class")=="")?"txt1":rglVx($aOpt,"class");
		if (rglVx($aOpt,"href")!="") {
			$tagA1 = "<a href=\"".rglVx($aOpt,"href")."\" class=\"$sClass\">";
			$tagA2 = "</a>";
		} else {
			$tagA1 = $tagA2 = "";
		}
		$sHtml = '';
		foreach($this->aCampiList as $kX =>$aCX) {
			$cols = $aCX['campo_size'];
			$val = rglVx($this->aCampiDB,$aCX['campo']);
			switch($aCX['campo_type']) {
				case 'data':
					$val = YMD2DMY($val);
					break;
				case 'numero':
					$val = num2str($val,$aCX['campo_dec'])."&nbsp;";
					break;
				case 'si/no':
					$val = ($val=='S')?'Si\'':'No';
					break;
				default:
					break;
			}			
			if ($tagA1!="" and trim($val)=="") {
				$val = "...";
			}
			$sStyle = rglVx($aOpt,"style");
			switch ($aCX['campo_style']) {
				case 'b':
					$sStyle .= "font-weight:bold;";
					break;
				case 'i':
					$sStyle .= "font-style:italic;";
					break;
				case 'ib':
				case 'bi':
					$sStyle .= "font-weight:bold;font-style:italic;";
					break;
			}
			switch ($aCX['campo_align']) {
				case 'destra':
					$sStyle .= "text-align:right;";
					break;
				case 'centro':
					$sStyle .= "text-align:center;";
					break;
				case 'sinistra':
				default:
					$sStyle .= "text-align:left;";
					break;
			}
			$sHtml .= "<td class=\"$sClass\" colspan=\"$cols\" style=\"$sStyle\">$tagA1$val$tagA2</td>";
			$tagA1 = $tagA2 = "";
		}
		return $sHtml;
	}
	
	public function setDocGenLab() {
		global $objDB;
		$this->oDocGen->nuovaRiga();
		$colDisp = 20;
		foreach($this->aCampiList as $kX =>$aCX) {			
			if ($colDisp > 0) {
				if ($aCX['campo_size']>0) {
					$cols = ($aCX['campo_size']<$colDisp)?$aCX['campo_size']:$colDisp;
					$aCol = array('cla'=>'lab1');
					$aCol['col'] = $cols*2;
					$aCol['alg'] = 'C';
					$aCol['txt'] = $aCX['label'];
					$this->oDocGen->setCol($aCol);
					$colDisp -= $cols;
				}
			}
		}
		if ($colDisp>0) {
			$aCol = array('cla'=>'txt1');
			$aCol['col'] = $colDisp*2;
			$aCol['alg'] = 'L';
			$aCol['txt'] = "";
			$this->oDocGen->setCol($aCol);
		}
		return true;
	}
	
	public function setDocGenDat($aRow) {
		global $objDB;
		$this->oDocGen->nuovaRiga();
		$colDisp = 20;
		foreach($this->aCampiList as $kX =>$aCX) {			
			if ($colDisp > 0) {
				if ($aCX['campo_size']>0) {
					$cols = ($aCX['campo_size']<$colDisp)?$aCX['campo_size']:$colDisp;
					$this->setDocGenCampo($cols, $aCX, $aRow);
					$colDisp -= $cols;
				}
			}
		}
		if ($colDisp>0) {
			$aCol = array('cla'=>'txt1');
			$aCol['col'] = $colDisp*2;
			$aCol['alg'] = 'L';
			$aCol['txt'] = "";
			$this->oDocGen->setCol($aCol);
		}
		return true;
	}
	
	private function setDocGenCampo($cols, $aCX, $aRow) {
		$sVal = $aRow[$aCX['campo']];
		switch($aCX['campo_type']) {
			case 'data':
				$sVal = YMD2DMY($sVal);
				break;
			case 'numero':
				$sVal = num2str($sVal,$aCX['campo_dec']);
				break;
			case 'si/no':
				$sVal = ($sVal=='S')?'Si\'':'No';
				break;
			default:
				break;
		}			
		$aCol = array('cla'=>'txt1');
		$aCol['col'] = $cols*2;
		$aCol['alg'] = 'L';
		$aCol['txt'] = $sVal;
		$this->oDocGen->setCol($aCol);
		return true;
	}
		
} //fine classe